package easy;

import java.util.Arrays;

public class Solution_303 {
    private int[] nums;
    public Solution_303(int[] nums) {
        this.nums = nums;
        System.out.println(sumRange(0, 5));
    }

    public int sumRange(int i, int j) {
        if (i==j) {
            return nums[i];
        }
        int[] target = new int[j-i+1];
        System.arraycopy(nums, i, target, 0, target.length);
        while (true) {
            if (target.length==2) {
                return target[0]+target[1];
            }
            int[] temp = new int[(target.length+1)/2];
            int length = target.length/2;

            Arrays.sort(target);
            for (int i1 = 0; i1 < length; i1++) {
                temp[i1] = target[i1] + target[target.length-i1-1];
            }
            if (temp.length!=length) {
                temp[length] = target[length];
            }
            target = temp;
        }
    }

    public static void main(String[] args) {
        new Solution_303(new int[]{-2, 0, 3, -5, 2, -1});
    }
}
